import { ref,onMounted, Ref } from 'vue'
import ApiTree from '~/api/ApiTree'
import CEvent from '~/utils/CEvent'
import DeviceUtil from '~/utils/DeviceUtil'
import StrUtil from '~/utils/StrUtil'
import System from '~/utils/System'
import VueTreeDetailMain from './detail/VueTreeDetailMain'
import VueTreeMain from './VueTreeMain'

export default class VueTreeDialog {

    static that:VueTreeDialog

    show:Ref<boolean> = ref(false)

    showMenu:Ref<boolean> = ref(false)

    form:Ref<{
        id?:any,
        name?:any,
        type?:any,
        pid?:any,
        pName?:any
    }> = ref({
    })

    formT:any = {}//临时存储form信息

    title:Ref<string> = ref("新增")
    /**
        0-新增，1-编辑，2-删除
     */
    status:Ref<number> = ref(0)

    constructor(){

        VueTreeDialog.that = this

        onMounted(()=>{
        })

    }


    addDevice(info:any){

        let that = VueTreeDialog.that

        that.title.value = "新增"
        that.status.value = 0
        that.form.value.type = "role"
        
        that.show.value = true

        that.form.value.id = ""
        that.form.value.pid = ""
        that.form.value.name = ""

        that.form.value.pid = info.id
        that.form.value.pName = info.name
        
    }

    editDevice(info:any){
        let that = VueTreeDialog.that
         
        that.title.value = "编辑"
        that.status.value = 1
        that.show.value = true

        that.form.value.id = info.id
        that.form.value.pid = info.pid
        that.form.value.name = info.name
        that.form.value.type = info.type

        this.formT = StrUtil.cp(that.form.value)

        let parentNode = StrUtil.getParent("id",info.id,VueTreeMain.that.tableData.value[0],"children")

        that.form.value.pName = parentNode.name
        // console.log("父节点",parentNode);
        
    }

    detailDevice(info:any){
        VueTreeDetailMain.id = info.id
        VueTreeMain.that.tableDataLoading.value = true
        setTimeout(() => {
            System.$router.push("/treeDetail")
        }, 500);
    }

    deleteDevice(info:any){

        let that = VueTreeDialog.that

        that.title.value = "删除"
        that.status.value = 2
        that.show.value = true
        that.form.value.id = info.id
        that.form.value.name = info.name

        console.log("删除")
    }
    
    submit(){
        let that = VueTreeDialog.that
        switch (that.status.value) {
            case 0:
               that.submitAdd()
                break;
            case 1:
               that.submitEdit()
                break;
            case 2:
               that.submitDelete()
                break;
            default:
                break;
        }
    }
    
    async submitAdd(){
        let that = VueTreeDialog.that
        let formValue = that.form.value
        await ApiTree.add(formValue.name).then((data:any)=>{
            StrUtil.sr("新增成功");
            that.show.value = false
            CEvent.emit("VuTreeMainGetList")
        }).catch((e:any)=>{
            StrUtil.er("新增失败");
        })
    }
    
    async submitEdit(){
        let that = VueTreeDialog.that
        let formValue = that.form.value

        let isOk = false
        await ApiTree.edit(parseInt(formValue.id),formValue.name).then((data:any)=>{
            StrUtil.sr("编辑成功");
            isOk = true
        }).catch((e:any)=>{
            StrUtil.er("编辑失败");
        })

        if(this.formT.pid!=formValue.pid && isOk){
            await ApiTree.editPid(parseInt(this.formT.id),formValue.pid).then((data:any)=>{
                // StrUtil.sr("修改父级成功");
                CEvent.emit("VuTreeMainGetList")
                that.show.value = false
            }).catch((e:any)=>{
                // StrUtil.er("编辑父级失败");
            })
        }else{
            CEvent.emit("VuTreeMainGetList")
            that.show.value = false
        }

    }
    
    async submitDelete(){
        let that = VueTreeDialog.that

        ApiTree.delete(parseInt(that.form.value.id)).then((data:any)=>{
            StrUtil.sr("删除成功");
            that.show.value = false
            CEvent.emit("VuTreeMainGetList")
        }).catch((e:any)=>{
            StrUtil.er("删除失败");
        })

    }

}